package com.passioneffect.contentmcs {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import com.passioneffect.components.ImageScroller;
	import com.passioneffect.display.ImageLoader;
	import com.passioneffect.events.PEEvent;
	import com.passioneffect.widgets.AutoPan;
	import com.passioneffect.widgets.Brightness;
	import com.passioneffect.widgets.Gray;
	import com.passioneffect.widgets.RectGrow;	

	/**
	 * @author Passion Effect
	 */
	public class ImageScrollerContent extends AbstractContentMovieClip {
		private var imageLoader : ImageLoader;
		private var rectGrow:RectGrow;
		private var autoPan : AutoPan;
		private var gray : Gray;
		private var draken : Brightness;
		private var rectGrow2 : RectGrow;
		private var rectGrow3 : RectGrow;
		
		public function ImageScrollerContent() {
			super();
		}
		
		public function onContentData(data:Object):void{
			this.data=data;
			imageLoader=new ImageLoader();
			imageLoader.autoSize=true;
			
			imageLoader.load(data.thumb,onLoaded);
			addChild(imageLoader);
			
			if(data.link){
				this.buttonMode=true;
			}
		}

		public function onInterValue(info:Object) : void {
			this.x=info.x;
			this.y=info.y;
		}

		private function onLoaded() : void {
			setConstraint(imageLoader.width,imageLoader.height);
			
			rectGrow=new RectGrow(imageLoader,-8,0x0000000,0.5);
			rectGrow2=new RectGrow(imageLoader,-4,ImageScroller(component).mouseOverBorderColor,1);
			rectGrow3=new RectGrow(imageLoader,-1,0x000000,1);
			
			autoPan=new AutoPan(imageLoader,3,1.1);
			draken=new Brightness(imageLoader,0.2,ImageScroller(component).mouseOverDarken);
			gray=new Gray(imageLoader,0.3,ImageScroller(component).mouseOverSaturation);
			
			setEvents();
		}
		
		private function setEvents() : void {
			imageLoader.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
			imageLoader.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
			imageLoader.addEventListener(MouseEvent.CLICK,clickHandler);
			
			component.addEventListener(PEEvent.MOUSE_OVER_ITEM, overItemHandler);	
			component.addEventListener(PEEvent.MOUSE_OUT_ITEM, outItemHandler);	
		}
		
		private function clickHandler(event : MouseEvent) : void {
			select();
			if(data.link){
				navigateToURL(new URLRequest(data.link),data["link@type"]);
			}
		}
		
		public function onSelect():void{
		}
		
		public function onUnselect():void{
		}
		
		private function outItemHandler(event : Event) : void {
			draken.close();
			gray.close();
		}

		private function overItemHandler(event : Event) : void {
			if(!isOver()){
				draken.open();
				gray.open();
			}
		}

		private function outHandler(event : MouseEvent) : void {
			out();
			rectGrow.close();
			rectGrow2.close();
			rectGrow3.close();
			autoPan.close();
		}

		private function overHandler(event : MouseEvent) : void {
			over();
			rectGrow.open();
			rectGrow2.open();
			rectGrow3.open();
			autoPan.open();
		}
		
		
		
	}
}
